<?php
class News_model_news extends Model {
    private $table_node_news = 'news';
    public function add($node_id, $published, $expired, $content) {
        $node_id = (int) $node_id;
        if ($node_id <= 0) {
            return false;
        }
        $published = (int) $published;
        $expired = (int)$expired;
        
        return $this->db->insert($this->table_node_news, array(
            'node_id'       => $node_id,
            'published'     => $published,
            'expired'       => $expired,
            'content'       => $content
        ));
    }
    
    
    public function edit($node_id, $published = 0, $expired = 0, $content = '') {
        
        $node_id = (int) $node_id;
        if ($node_id <= 0) {
            tos_die('New update error');
        }
        
        $published = (int) $published;
        $expired = (int) $expired;
        $content = trim($content);
        
        $data = array();
        if ($published) {
            $data['published'] = $published;
        }
        if ($expired) {
            $data['expired'] = $expired;
        }
        if ( ! empty($content)) {
            $data['content'] = $content;
        }
        
        return $this->db->update($this->table_node_news, $data, array('node_id' => $node_id));
    }
    
    
    public function entry($node_id) {
        $node_id = (int) $node_id;
        $row = array();
        if ($node_id > 0) {
            $sql = "SELECT * FROM `{$this->table_node_news}` WHERE `node_id`='{$node_id}' LIMIT 1";
            $row = $this->db->row($sql);
        }
        return $row;
    }
}
?>